package com.css.fxfzdpzs.modules.aqbz.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzdpzs.base.response.RestResponse;
import com.css.fxfzdpzs.modules.aqbz.entity.first.AbMapDate;
import com.css.fxfzdpzs.modules.aqbz.entity.second.*;
import com.css.fxfzdpzs.modules.aqbz.service.AqbzService;
import com.css.fxfzdpzs.modules.aqbz.vo.AbMapDateEntity;
import com.css.fxfzdpzs.modules.aqbz.vo.AbMapDateVO;
import com.css.fxfzdpzs.modules.aqbz.vo.AqbzSecondVO;
import com.css.fxfzdpzs.modules.aqbz.vo.AqbzSysUsageSituationVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.List;

/**
 * @author y1fan
 * @description  aqbz  大屏获取接口
 */
@RestController
@RequestMapping("/aqbz")
@Slf4j
public class AqbzController {

    @Resource
    private AqbzService aqbzService;

    /**
     * 安保首页 （一级目录）
     *
     * @return
     */
    @GetMapping("/getActivityMap")
    public RestResponse getActivityMap() {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            AqbzSysUsageSituationVO aqbzSysUsageSituationVO = aqbzService.getActivityMap();
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(aqbzSysUsageSituationVO);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }


    /**
     * 安保首页 （二级目录）
     *
     * @return
     */
    @GetMapping("/getSecond")
    public RestResponse getSecond(String iaId) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            AqbzSecondVO aqbzSecondVO = aqbzService.getSecond(iaId);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(aqbzSecondVO);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }


    /**
     * 安保（二级目录） 地震灾害风险隐患点
     *
     * @return
     */
    @GetMapping("/getDangerPoints")
    public RestResponse getDangerPoints(String iaId) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            List<AbRiskDangerPoints> abRiskDangerPoints = aqbzService.getDangerPoints(iaId);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(abRiskDangerPoints);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 安保（二级目录） 严重破坏和毁坏风险占比统计
     *
     * @return
     */
    @GetMapping("/getProportionStatistics")
    public RestResponse getProportionStatistics(String iaId) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            List<AbRiskProportionStatistics> abRiskProportionStatistics= aqbzService.getProportionStatistics(iaId);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(abRiskProportionStatistics);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 安保（二级目录） 图鉴展示
     *
     * @return
     */
    @GetMapping("/getTjzs")
    public RestResponse getTjzs(String iaId) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            List<AbTujian> tjzs= aqbzService.getTjzs(iaId);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(tjzs);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

   // private AbReport report;//报告信息

    /**
     * 安保（二级目录） 报告信息
     *
     * @return
     */
    @GetMapping("/getReport")
    public RestResponse getReport(String iaId) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            AbReport report= aqbzService.getReport(iaId);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(report);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }


    /**
     * 安保（二级目录） 重点场所
     *
     * @return
     */
    @GetMapping("/getZdcs")
    public RestResponse getZdcs(String iaId) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            List<AbKeySite> zdcs= aqbzService.getZdcs(iaId);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(zdcs);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 安保（二级目录） 重点场所
     *
     * @return
     */
    @GetMapping("/getLittleZdcs")
    public RestResponse getLittleZdcs(String zdcsId) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            List<AbLittleZdcs> zdcs= aqbzService.getLittleZdcs(zdcsId);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(zdcs);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 安保首页地图信息
     *
     * @return
     */
    @GetMapping("/getMapDate")
    public RestResponse getMapDate() {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            AbMapDateVO zdcs= aqbzService.getMapDate();
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(zdcs);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }
    /**
     * 安保首页国家级 省级 列表
     *
     * @return
     */
    @GetMapping("/getIa")
    public RestResponse getIa(String provinceCode,String type) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            List<AbMapDate> list= aqbzService.getIa(provinceCode,type);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(list);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 文件下载
     * @param url 附件路径
     */
    @GetMapping("/downLoadFile")
    public void downloadFile(String url,String name) {
        OutputStream os = null;
        InputStream is = null;
        try{

            if (org.apache.commons.lang3.StringUtils.isNotBlank(url)) {
                // 防乱码
//                    String name =
//                        new String(realFileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
                ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
                assert requestAttributes != null;
                HttpServletResponse response = requestAttributes.getResponse();
                assert response != null;
                response.reset();
                response.setContentType("application/octet-stream");
                response.setHeader("Content-Disposition", "attachment;filename=\"" + URLEncoder.encode(name+".docx","UTF-8") + "\"");
                // 跨域
                response.setHeader("Access-Control-Allow-Origin", "*");
                response.setHeader("Access-Control-Allow-Method", "POST,GET");
                is = new FileInputStream(new File(url));

                // PrintWrite pw=response.getWriter();  或
                os = response.getOutputStream();
                int flag = 0;
                while ((flag = is.read()) != -1) {
                    os.write(flag);
                }
            }
        } catch (Exception e){
            e.printStackTrace();
            System.out.println(e.getMessage());
        } finally{
            try{
                if(is != null){
                    is.close();
                }
                if(os != null){
                    os.flush();
                    os.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
